package com.openroom.ui;

import java.lang.reflect.Field;

import org.apache.pivot.beans.BXML;
import org.apache.pivot.collections.Map;

public class NamespaceHelper {

    public static void initialize(Map<String, Object> namespace,
            Object instance) {
        try {
            Class<?> clazz = instance.getClass();
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field field : declaredFields) {
                String name = field.getName();
                Object object = namespace.get(name);
                if (object != null) {
                    if (field.getAnnotation(BXML.class) != null) {
                        field.setAccessible(true);
                        try {
                            field.set(instance, object);
                        } catch (Exception e) {
                            // Supress and move on
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
